//
//  TTImageDingzhiViewController.m
//  TaoTuo
//
//  Created by 刘广 on 2020/3/27.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTImageDingzhiViewController.h"
#import "TTImageDingzhiUploadView.h"
#import "TTFiltrateView.h"
#import "TTImageDingzhiChooseView.h"
#import "WSDatePickerView.h"
#import "THScrollChooseView.h"

@interface TTImageDingzhiViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *qbLabel;
@property (nonatomic, strong) TTImageDingzhiUploadView *uploadView;

//@property (nonatomic, strong) UILabel *czLabel;
//@property (nonatomic, strong) TTFiltrateView *czFilteView;

@property (nonatomic, strong) TTImageDingzhiChooseView *czChooseView;
@property (nonatomic, strong) TTImageDingzhiChooseView *dzChooseView;
@property (nonatomic, strong) TTImageDingzhiChooseView *xkChooseView;
@property (nonatomic, strong) TTImageDingzhiChooseView *scChooseView;
@property (nonatomic, strong) TTImageDingzhiChooseView *njChooseView;
@property (nonatomic, strong) TTImageDingzhiChooseView *timeChooseView;

@property (nonatomic, strong) UILabel *kzLabel;
@property (nonatomic, strong) UITextField *kzTF;

@property (nonatomic, strong) UILabel *bzLabel;
@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UILabel *countStrLabel;
@property (nonatomic, strong) UIButton *minusBtn;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) UIButton *addBtn;

@property (nonatomic, strong) UILabel *zgLabel;
@property (nonatomic, strong) UIButton *jgBtn;
@property (nonatomic, strong) UIButton *xjbBtn;

///做工
@property (nonatomic, copy) NSString *zgName;

//@property (nonatomic, assign) BOOL scHide;
//@property (nonatomic, assign) BOOL njHide;

@end

@implementation TTImageDingzhiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"来图定制";
    
    [self setupSubviews];
    
}

- (void)czChooseAction
{
    [self.view endEditing:YES];
    NSArray *questionArray = @[@"18K白", @"18K红", @"18K黄", @"18K双色", @"PT900", @"PT950"];
    THScrollChooseView *scrollChooseView = [[THScrollChooseView alloc] initWithQuestionArray:questionArray withDefaultDesc:@""];
    [scrollChooseView showView];
    scrollChooseView.confirmBlock = ^(NSInteger selectedQuestion) {
        self.czChooseView.content = questionArray[selectedQuestion];
    };
}

- (void)dzChooseAction
{
    [self.view endEditing:YES];
    NSArray *questionArray = @[@"女戒", @"男戒", @"对戒", @"套链", @"吊坠", @"手链", @"手镯", @"耳钉", @"耳线", @"耳坠"];
    THScrollChooseView *scrollChooseView = [[THScrollChooseView alloc] initWithQuestionArray:questionArray withDefaultDesc:@""];
    [scrollChooseView showView];
    scrollChooseView.confirmBlock = ^(NSInteger selectedQuestion) {
        NSString *str = questionArray[selectedQuestion];
        self.dzChooseView.content = str;
        if ([str isEqualToString:@"手镯"]) {
            self.scChooseView.hidden = YES;
            self.njChooseView.hidden = NO;
            self.scChooseView.content = @"";
        } else if ([str isEqualToString:@"女戒"] || [str isEqualToString:@"男戒"] || [str isEqualToString:@"对戒"]) {
            self.scChooseView.hidden = NO;
            self.njChooseView.hidden = YES;
            self.njChooseView.content = @"";
        } else { // if ([str isEqualToString:@"套链"])
            self.scChooseView.hidden = YES;
            self.njChooseView.hidden = YES;
            self.scChooseView.content = @"";
            self.njChooseView.content = @"";
        }
        [self.view setNeedsLayout];
    };
}

- (void)xkChooseAction
{
    [self.view endEditing:YES];
    NSArray *questionArray = @[@"1-2分", @"5分", @"10分", @"15分", @"20分", @"25分", @"30分", @"35分", @"40分", @"45分", @"50分", @"60分", @"70分", @"75分", @"80分", @"90分", @"100分", @"150分", @"200分", @"300分", @"400分", @"500分"];
    THScrollChooseView *scrollChooseView = [[THScrollChooseView alloc] initWithQuestionArray:questionArray withDefaultDesc:@""];
    [scrollChooseView showView];
    scrollChooseView.confirmBlock = ^(NSInteger selectedQuestion) {
        self.xkChooseView.content = questionArray[selectedQuestion];

    };
}

- (void)scChooseAction
{
    [self.view endEditing:YES];
    NSArray *questionArray = @[@"7#", @"8#", @"9#", @"10#", @"11#", @"12#", @"13#", @"14#", @"15#", @"16#", @"17#", @"18#", @"19#", @"20#", @"21#", @"22#", @"23#", @"24#", @"25#", @"26#", @"27#", @"28#"];
    THScrollChooseView *scrollChooseView = [[THScrollChooseView alloc] initWithQuestionArray:questionArray withDefaultDesc:@""];
    [scrollChooseView showView];
    scrollChooseView.confirmBlock = ^(NSInteger selectedQuestion) {
        self.scChooseView.content = questionArray[selectedQuestion];

    };
}

- (void)njChooseAction
{
    [self.view endEditing:YES];
    NSArray *questionArray = @[@"60-64", @"64-68", @"68-72", @"72-76", @"76-80", @"80-84", @"84-86"];
    THScrollChooseView *scrollChooseView = [[THScrollChooseView alloc] initWithQuestionArray:questionArray withDefaultDesc:@""];
    [scrollChooseView showView];
    scrollChooseView.confirmBlock = ^(NSInteger selectedQuestion) {
        self.njChooseView.content = questionArray[selectedQuestion];
    };
}

- (void)timeChooseAction
{
    [self.view endEditing:YES];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyy-MM-dd"];
    NSString *nowDate = [formatter stringFromDate:[NSDate date]];
    
    NSDate *afterDate = [[NSDate date] dateByAddingDays:15];
    
    @weakify(self)
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay minLimitDateString:nowDate maxLimitDateString:@"2100-1-1" scrollToDate:afterDate CompleteBlock:^(NSDate *selectDate) {
        @strongify(self)
        NSString *date = [selectDate stringWithFormat:@"yyyy-MM-dd"];
        self.timeChooseView.content = date;
    }];
    datepicker.doneButtonColor = kMainColor;
    datepicker.dateLabelColor = kMainColor;
    datepicker.hideBackgroundYearLabel = YES;
    [datepicker show];
}


- (void)minusBtnClick
{
    NSInteger count = self.countLabel.text.integerValue;
    count--;
    
    if (count < 1) {
        self.minusBtn.backgroundColor = UIColorHex(#F0F4F7);
        return;
    }
    
    self.minusBtn.backgroundColor = count == 1 ? UIColorHex(#F0F4F7) : kMainColor;
    
    self.countLabel.text = [NSString stringWithFormat:@"%ld", count];
    
}

- (void)addBtnClick
{
    NSInteger count = self.countLabel.text.integerValue;
    count++;
    self.countLabel.text = [NSString stringWithFormat:@"%ld", count];
    
    if (count > 1) {
        self.minusBtn.backgroundColor = kMainColor;
    }
}

- (void)jgBtnClick:(UIButton *)sender
{
    sender.selected = YES;
    self.xjbBtn.selected = NO;
    
    self.zgName = [sender.titleLabel.text removeBlank];
}

- (void)xjbBtnClick:(UIButton *)sender
{
    sender.selected = YES;
    self.jgBtn.selected = NO;
    
    self.zgName = [sender.titleLabel.text removeBlank];
}

// 提交
- (void)commitbtnClick
{
    if (self.uploadView.images.count == 0) {
        [self.view makeToast:@"请上传图片或短视频"];
        return;
    }
    if (self.czChooseView.content.length == 0) {
        [self.view makeToast:@"请选择材质"];
        return;
    }
    if (self.dzChooseView.content.length == 0) {
        [self.view makeToast:@"请选择定制品类"];
        return;
    }
    if (self.xkChooseView.content.length == 0) {
        [self.view makeToast:@"请选择镶口大小"];
        return;
    }
    if (!self.scChooseView.hidden && self.scChooseView.content.length == 0) {
        [self.view makeToast:@"请选择手寸"];
        return;
    }
    if (!self.njChooseView.hidden && self.njChooseView.content.length == 0) {
        [self.view makeToast:@"请选择内径"];
        return;
    }
    if (self.timeChooseView.content.length == 0) {
        [self.view makeToast:@"请选择收货时间"];
        return;
    }
    
    NSMutableDictionary *dic = @{
        @"GoldColor" : self.czChooseView.content,
        @"CategoryName" : self.dzChooseView.content,
        @"InsertSize" : self.xkChooseView.content,
        @"HandSize" : self.scChooseView.content,
        @"BraceletSize" : self.njChooseView.content,
        @"ReceiveTime" : self.timeChooseView.content,
        @"Lettering" : self.kzTF.text,
        @"Content" : self.textView.text,
        @"Number" : self.countLabel.text,
        @"GoodsGrade" : self.zgName,
    }.mutableCopy;
    [dic setObject:self.uploadView.images forKey:@"Images"];
    
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postJsonWithUrlString:commitCustomized_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
                   
        [MBProgressHUD showSuccess:@"提交成功"];
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    NSLog(@"asdgljkh ");
    
    CGFloat leftW = 100;
    CGFloat h = 35;
    self.qbLabel.frame = CGRectMake(10, 10, leftW, h);
    self.uploadView.frame = CGRectMake(self.qbLabel.right, 0, self.uploadView.width, self.uploadView.height);
//    self.czLabel.frame = CGRectMake(self.qbLabel.left, self.uploadView.bottom + 10, leftW, h);
//    self.czFilteView.frame = CGRectMake(0, self.czLabel.bottom, self.view.width, 40);
    
    self.czChooseView.top = self.uploadView.bottom + 10;
    self.dzChooseView.top = self.czChooseView.bottom + 10;
    self.xkChooseView.top = self.dzChooseView.bottom + 10;
    self.scChooseView.top = self.xkChooseView.bottom + 10;
    self.njChooseView.top = self.xkChooseView.bottom + 10;
    
    CGFloat timeTop = (self.scChooseView.hidden && self.njChooseView.hidden) ? self.xkChooseView.bottom + 10 : self.scChooseView.bottom + 10;
    
    self.timeChooseView.top = timeTop;
    
    self.kzLabel.frame = CGRectMake(10, self.timeChooseView.bottom + 10, leftW, h);
    self.kzTF.frame = CGRectMake(self.kzLabel.right, self.kzLabel.top, self.view.width - 120, h);
    
    self.bzLabel.frame = CGRectMake(10, self.kzLabel.bottom + 10, leftW, h);
    self.textView.frame = CGRectMake(self.kzTF.left, self.bzLabel.top, self.kzTF.width, 100);
    
    self.countStrLabel.frame = CGRectMake(10, self.textView.bottom + 10, leftW, h);
    self.minusBtn.frame = CGRectMake(self.textView.left, self.countStrLabel.top, 30, 30);
    self.countLabel.frame = CGRectMake(self.minusBtn.right + 5, self.countStrLabel.top, 60, 30);
    self.addBtn.frame = CGRectMake(self.countLabel.right + 5, self.countStrLabel.top, 30, 30);
    
    
    self.zgLabel.frame = CGRectMake(10, self.countStrLabel.bottom + 10, leftW, h);
    self.jgBtn.frame = CGRectMake(self.minusBtn.left, self.zgLabel.top, 70, h);
    self.xjbBtn.frame = CGRectMake(self.jgBtn.right + 10, self.zgLabel.top, 70, h);
    
    self.scrollView.contentSize = CGSizeMake(self.view.width, self.xjbBtn.bottom + 120);
    
}

- (void)setupSubviews
{
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//    _scrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_scrollView];
    
    _qbLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:15];
    _qbLabel.attributedText = [self getAttrStringWithString:@"*起版图"];
    [self.scrollView addSubview:_qbLabel];
    
    self.uploadView = [[TTImageDingzhiUploadView alloc] initWithFrame:CGRectMake(0, 0, self.view.width - 120, 90)];
    [self.scrollView addSubview:self.uploadView];
    @weakify(self)
    self.uploadView.getContentHeightBlock = ^(CGFloat height) {
        @strongify(self)
        [self.view setNeedsLayout];
    };
    
//    _czLabel = [UILabel labelWithTitle:@"材质" color:UIColorHex(333333) fontSize:15];
//    [self.scrollView addSubview:_czLabel];
//
//    _czFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
//        @strongify(self)
//        self.czName = selectedArray.firstObject;
//
//    } leftRightSpace:0];
//    NSArray *czNamesArray = @[@"18K白", @"18K红", @"18K黄", @"18K双色", @"PT900", @"PT950"];
//    _czFilteView.dataArray = czNamesArray;
//    [self.scrollView addSubview:_czFilteView];
//    [_czFilteView setSelectedFirstItem];
    
    CGFloat h = 35;
    
    _czChooseView = [[TTImageDingzhiChooseView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, h) title:@"*材质" target:self action:@selector(czChooseAction)];
    _czChooseView.content = @"18K白";
    [self.scrollView addSubview:_czChooseView];
    
    _dzChooseView = [[TTImageDingzhiChooseView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, h) title:@"*定制品类" target:self action:@selector(dzChooseAction)];
    _dzChooseView.content = @"女戒";
    [self.scrollView addSubview:_dzChooseView];
    
    _xkChooseView = [[TTImageDingzhiChooseView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, h) title:@"*镶口大小" target:self action:@selector(xkChooseAction)];
    [self.scrollView addSubview:_xkChooseView];
    
    _scChooseView = [[TTImageDingzhiChooseView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, h) title:@"*需要手寸" target:self action:@selector(scChooseAction)];
    [self.scrollView addSubview:_scChooseView];
    
    _njChooseView = [[TTImageDingzhiChooseView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, h) title:@"*需要内径" target:self action:@selector(njChooseAction)];
    _njChooseView.hidden = YES;
    [self.scrollView addSubview:_njChooseView];
    
    _timeChooseView = [[TTImageDingzhiChooseView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, h) title:@"*收货时间" target:self action:@selector(timeChooseAction)];
    [self.scrollView addSubview:_timeChooseView];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyy-MM-dd"];
    NSDate *afterDate = [[NSDate date] dateByAddingDays:15];
    NSString *nowDateStr = [formatter stringFromDate:afterDate];
    _timeChooseView.content = nowDateStr;
    
    _kzLabel = [UILabel labelWithTitle:@"刻字" color:UIColorHex(333333) fontSize:15];
    [self.scrollView addSubview:_kzLabel];
    
    _kzTF = [[UITextField alloc] init];
    _kzTF.layer.cornerRadius = 5;
    _kzTF.font = [UIFont systemFontOfSize:15];
    _kzTF.backgroundColor = UIColorHex(#F0F4F7);
    [self.scrollView addSubview:_kzTF];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
    _kzTF.leftView = leftView;
    _kzTF.leftViewMode = UITextFieldViewModeAlways;
    
    _bzLabel = [UILabel labelWithTitle:@"备注" color:UIColorHex(333333) fontSize:15];
    [self.scrollView addSubview:_bzLabel];
    
    _textView = [[UITextView alloc] init];
    _textView.font = [UIFont systemFontOfSize:15];
    _textView.layer.cornerRadius = 5;
    _textView.backgroundColor = UIColorHex(#F0F4F7);
    [self.scrollView addSubview:_textView];
    
    _countStrLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:15];
    _countStrLabel.attributedText = [self getAttrStringWithString:@"*件数"];
    [self.scrollView addSubview:_countStrLabel];
    
    _minusBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"minus"] target:self action:@selector(minusBtnClick)];
    _minusBtn.backgroundColor = UIColorHex(#F0F4F7);
    _minusBtn.layer.cornerRadius = 2;
    [self.scrollView addSubview:_minusBtn];
    
    _countLabel = [UILabel labelTextCenterWithTitle:@"1" color:UIColorHex(333333) fontSize:15];
    _countLabel.layer.cornerRadius = 5;
    _countLabel.backgroundColor = UIColorHex(#F0F4F7);
    [self.scrollView addSubview:_countLabel];
    
    _addBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"add_dz"] target:self action:@selector(addBtnClick)];
    _addBtn.backgroundColor = kMainColor;
    _addBtn.layer.cornerRadius = 2;
    [self.scrollView addSubview:_addBtn];
    
    
    _zgLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:15];
    _zgLabel.attributedText = [self getAttrStringWithString:@"*做工"];
    [self.scrollView addSubview:_zgLabel];
    
    _jgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _jgBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_jgBtn setTitle:@" 精工" forState:UIControlStateNormal];
    [_jgBtn setTitleColor:UIColorHex(#666666) forState:UIControlStateNormal];
    [_jgBtn setImage:[UIImage imageNamed:@"luoshi_unsel"] forState:UIControlStateNormal];
    [_jgBtn setImage:[UIImage imageNamed:@"luoshi_sel"] forState:UIControlStateSelected];
    [_jgBtn addTarget:self action:@selector(jgBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:_jgBtn];
    _jgBtn.selected = YES;
    
    
    _xjbBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _xjbBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_xjbBtn setTitle:@" 性价比" forState:UIControlStateNormal];
    [_xjbBtn setTitleColor:UIColorHex(#666666) forState:UIControlStateNormal];
    [_xjbBtn setImage:[UIImage imageNamed:@"luoshi_unsel"] forState:UIControlStateNormal];
    [_xjbBtn setImage:[UIImage imageNamed:@"luoshi_sel"] forState:UIControlStateSelected];
    [_xjbBtn addTarget:self action:@selector(xjbBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:_xjbBtn];
    
    
    UIButton *commitbtn = [UIButton createButtonWithTitle:@"提交定制" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:18] backgroundColor:kMainColor target:self action:@selector(commitbtnClick)];
    [self.view addSubview:commitbtn];
    [commitbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(45);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
    }];
    
}

- (NSMutableAttributedString *)getAttrStringWithString:(NSString *)string
{
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:string];
    if ([string hasPrefix:@"*"]) {
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[string rangeOfString:@"*"]];
    }
    
    return attr;
    
}

@end
